﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 可编辑的文本
    /// </summary>
    public class EditableText : Control
    {
        static EditableText()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(EditableText), new FrameworkPropertyMetadata(typeof(EditableText)));
        }
        public EditableText()
        {
        }
        private TextBlock textBlock;
        private TextBox textBox;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.textBlock = (TextBlock)this.GetTemplateChild("textBlock");
            this.textBox = (TextBox)this.GetTemplateChild("textBox");
            this.textBox.LostFocus += TextBox_LostFocus;
            textBox.KeyDown += TextBox_KeyDown;
            if (this.IsEditing && this.IsEnabled)
            {
                BeginEdit();
            }
            else
            {
                CompleteEdit();
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.IsEditing = false;
            }
            else if (e.Key == Key.Escape)
            {
                CompleteEdit(true);
                this.IsEditing = false;
            }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.IsEditing = false;
        }
        /// <summary>
        /// 开始编辑
        /// </summary>
        protected void BeginEdit()
        {
            textBox.Text = this.Text;//开始编辑时重置文本框内的文本
            textBlock.Visibility = Visibility.Collapsed;
            textBox.Visibility = Visibility.Visible;
            textBox.Focus();
            textBox.SelectAll();
        }
        /// <summary>
        /// 完成/结束标记
        /// </summary>
        /// <param name="cancel"></param>
        protected void CompleteEdit(bool cancel = false)
        {
            if (!cancel)
            {
                this.SetCurrentValue(TextProperty, textBox.Text);
            }
            else
            {
                textBox.SetCurrentValue(TextBox.TextProperty, this.Text);
            }

            textBox.Select(0, 0);
            textBlock.Visibility = Visibility.Visible;
            textBox.Visibility = Visibility.Collapsed;
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(EditableText), new PropertyMetadata(null));

        public TextAlignment TextAlignment
        {
            get { return (TextAlignment)GetValue(TextAlignmentProperty); }
            set { SetValue(TextAlignmentProperty, value); }
        }

        public static readonly DependencyProperty TextAlignmentProperty =
            DependencyProperty.Register("TextAlignment", typeof(TextAlignment), typeof(EditableText), new PropertyMetadata(TextAlignment.Left));

        public TextDecorationCollection TextDecorations
        {
            get { return (TextDecorationCollection)GetValue(TextDecorationsProperty); }
            set { SetValue(TextDecorationsProperty, value); }
        }

        public static readonly DependencyProperty TextDecorationsProperty =
            DependencyProperty.Register("TextDecorations", typeof(TextDecorationCollection), typeof(EditableText), new PropertyMetadata(default(TextDecorationCollection)));

        /// <summary>
        /// 是否正处于编辑模式
        /// </summary>
        public bool IsEditing
        {
            get { return (bool)GetValue(IsEditingProperty); }
            set { SetValue(IsEditingProperty, value); }
        }

        public static readonly DependencyProperty IsEditingProperty =
            DependencyProperty.Register("IsEditing", typeof(bool), typeof(EditableText), new PropertyMetadata(false));

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if ((e.Property == IsEditingProperty || e.Property == IsEnabledProperty) && textBox != null)
            {
                if (this.IsEditing && this.IsEnabled)
                {
                    BeginEdit();
                }
                else
                {
                    CompleteEdit();
                }
            }
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.ChangedButton == MouseButton.Left)
            {
                this.CaptureMouse();
            }

        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            if (this.IsMouseCaptured && e.ChangedButton == MouseButton.Left)
            {
                this.OnClick();
            }
            this.ReleaseMouseCapture();
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            this.ReleaseMouseCapture();
        }

        /// <summary>
        /// 在用户单击此控件时触发
        /// </summary>
        public event RoutedEventHandler Click
        {
            add { AddHandler(ClickEvent, value); }
            remove { RemoveHandler(ClickEvent, value); }
        }

        public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent(
        "Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EditableText));

        protected virtual void OnClick()
        {
            RoutedEventArgs args = new RoutedEventArgs();
            args.RoutedEvent = EditableText.ClickEvent;
            this.RaiseEvent(args);
        }

    }
}
